﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;

namespace Zhaoxi.AgiletyFramework.WebCore.SwaggerExtend
{
    /// <summary>
    /// 提供静态方法，静态类不可实例化
    /// </summary>
    public static class SwaggerExtensions
    {
        /// <summary>
        /// 配置Swagger，this表示这是一个扩展方法
        /// </summary>
        /// <param name="service"></param>
        /// <param name="docName">文档名称</param>
        /// <param name="docDescription">文档描述</param>
        public static void AddSwaggerExt(this IServiceCollection service, string docName = "星星后台管理系统", string? docDescription = $"星星后台管理系统")
        {
            // 由于NET7版本太旧，所以安装程序包时一定不要安装最新稳定版，而应该安装旧版本
            service.AddEndpointsApiExplorer();
            service.AddSwaggerGen(u =>
            {
                // 多版本文档配置
                foreach (var version in typeof(ApiVersions).GetEnumNames())
                {
                    u.SwaggerDoc(version, new OpenApiInfo
                    {
                        Version = version,//版本
                        Title = docName,//标题
                        Description = docDescription + $"版本{version}",//描述
                        // 联系信息
                        Contact = new OpenApiContact
                        {
                            Name = "王华凯",
                            Url = new Uri("https://www.baidu.com")
                        }
                    });
                }


                // 项目xml文档绝对路径
                var file = Path.Combine(AppContext.BaseDirectory, $"{AppDomain.CurrentDomain.FriendlyName}.xml");
                // 显示控制器层注解
                u.IncludeXmlComments(file, true);
                // 对action的名称进行排序，如果有多个，就可以看见效果了
                u.OrderActionsBy(o => o.RelativePath);

                #region 支持jwt Token授权
                // 添加安全定义 - 配置支持Token授权机制
                u.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "请输入Token，格式为 Bearer {token} (注意中间必须有空格)",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey,
                    BearerFormat = "JWT",
                    Scheme = "Bearer"
                });

                // 添加全局安全要求
                u.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id = "Bearer"
                            }
                        },
                        new string[] { }
                    }
                });
                #endregion
            });


        }

        /// <summary>
        /// 使用Swagger
        /// </summary>
        /// <param name="app"></param>
        /// <param name="docName">文档名称</param>
        public static void UseSwaggerExt(this WebApplication app, string docName = "星星后台管理系统")
        {
            app.UseSwagger();
            app.UseSwaggerUI(u =>
            {
                // 使用循环实现多版本文档
                foreach (var version in typeof(ApiVersions).GetEnumNames())
                {
                    u.SwaggerEndpoint($"/swagger/{version}/swagger.json", docName + " 版本：" + version);
                }
            });
        }
    }
}
